/** 龙与地下城2

  By setycyas @2023-02-08
*/

/* 全局设定
*/
#Warn
#MaxThreadsPerHotkey 1
SetWorkingDir(A_ScriptDir)
#Include "..\Class\FaceDetector.2.ahk"
#Include "..\Class\CommonMacro.2.ahk"
#Include "..\Class\CommonUI.2.ahk"
SendMode("Input")

/* 制作龙与地下城2的宏类
*/
class DND2Macro extends CommonMacro {

  /* 新增的宏函数
  */
  _牧师三连(key){ ;前a下前a-下上a,按住键才会持续出招,距离太近反而连不上
    local thisKey := MyFuns.getHotkey(key) ;触发技能的热键
    this._前A(key)
    sleep(250)
    this._下前A(thisKey)
    sleep(100)
    if (!GetKeyState(thisKey, "P")) {
      return
    }
    this._下上A(thisKey)
  }
  _小跳(key){
    this._press(2)
    sleep(50)
    this._press(2)
    sleep(50)
  }
  _快速旋转A(key){
    static delays := [20, 20, 20, 20, 20]
    local thisKey := MyFuns.getHotkey(key) ;触发技能的热键
    local keys := ["up", "right", "down", "left", this._keys[1]]
    while (GetKeyState(thisKey, "P")) {
      MyFuns.inputSkill(keys, delays)
    }
  }
  _盗贼空中滑行(key){ ;空中使用
    local back := this._face.back()
    MyFuns.pressKeys(["down", this._keys[1]], [20, 20])
    sleep(20)
    MyFuns.pressKeys([back, this._keys[2], this._keys[1]], [20, 20, 20])
    this._face.setFace(back)
  }
  _矮人解剑(key){ ;效果貌似还不错
    this._down(2)
    sleep(50)
    this._up(2)
    sleep(730)
    this._press(1)
  }
  _精灵解剑(key){
    this._down(2)
    sleep(50)
    this._up(2)
    sleep(820)
    this._press(1)
  }
  _战士解剑(key){
    this._down(2)
    sleep(50)
    this._up(2)
    sleep(840)
    this._press(1)
  }
  /* 实践证明小跳容易失误,参数480左右看着已经很接近但怎么调整都是失误率很高
  _精灵小跳版边解剑(key){
    this._press(2)
    sleep(50)
    this._press(2)
    sleep(this.testParams[1])
    this._press(1)
  }
  */

  /* 重载宏接口
  */
  _setMacro(){
    super._setMacro()
    this.macroMap["牧师三连"] := this._牧师三连.bind(this)
    this.macroMap["小跳"] := this._小跳.bind(this)
    this.macroMap["快速旋转A"] := this._快速旋转A.bind(this)
    this.macroMap["盗贼空中滑行"] := this._盗贼空中滑行.bind(this)
    this.macroMap["矮人解剑"] := this._矮人解剑.bind(this)
    this.macroMap["精灵解剑"] := this._精灵解剑.bind(this)
    this.macroMap["战士解剑"] := this._战士解剑.bind(this)
    this.macroArr := MyFuns.arrCombine(this.macroArr, ["牧师三连", "小跳", "快速旋转A", "盗贼空中滑行",
      "矮人解剑", "精灵解剑", "战士解剑"])
  }

}

/* 运行当前脚本
*/
if (A_ScriptName == "龙与地下城2.2.ahk") {
  face := FaceDetector() ;方向检测对象
  macro := DND2Macro(face, ["x","e"]) ;宏对象,有界面时第二个参数可以随便写但长度不要小于2
  ;宏方案预设,用于菜单
  preset := Map("有盾", ["连发A", "下前A", "下上A", "A后防御", "下前B", "小跳", "快速旋转A"],
    "牧师", ["连发A", "下前A", "牧师三连", "A后防御", "下前B", "小跳", "快速旋转A"],
    "法师", ["连发A", "下前A", "小跳", "小跳", "下前B", "小跳", "快速旋转A"],
    "盗贼", ["连发A", "下前A", "下上A", "小跳", "下前B", "盗贼空中滑行", "快速旋转A"])
  ;新建界面并设定
  ui := CommonUI("龙与地下城2", macro ,preset)
  ui.setup()
}